﻿using MediatR;
using RBAC.ApiWrites.Applocation.Command;
using RBAC.Domain.RbacEntity;
using RBAC.Infrasturcture.Interfaces;
using RBAC.ResultCode;
using System.IO;

namespace RBAC.ApiWrites.Applocation.Commandhandler
{
    public class UpdateRoleCommandhandler : IRequestHandler<UpdateRoleCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<Permission> pemissionrepository;
        private readonly IBaseRepository<Role> rolerepository;
        private readonly IBaseRepository<RolePermission> rolepermissionrepository;
        private readonly ILogger<UpdateRoleCommandhandler> logger;

        public UpdateRoleCommandhandler(IBaseRepository<Permission> pemissionrepository,IBaseRepository<Role> rolerepository,IBaseRepository<RolePermission> rolepermissionrepository,ILogger<UpdateRoleCommandhandler> logger)
        {
            this.pemissionrepository = pemissionrepository;
            this.rolerepository = rolerepository;
            this.rolepermissionrepository = rolepermissionrepository;
            this.logger = logger;
        }
        public async Task<ApiResult<int>> Handle(UpdateRoleCommand request, CancellationToken cancellationToken)
        {

            using (var tran = rolerepository.DB.Database.BeginTransaction()) 
            {
                try
                {
                    var roleinfo = rolerepository.GetAll().FirstOrDefault(x => x.RoleId == request.RoleId);
                    roleinfo.CreateTime = DateTime.Now;
                    roleinfo.Status = 1;
                    roleinfo.RoleName = request.RoleName;
                    var res = await rolerepository.UpdateAsync(roleinfo);
                    var permissioninfo = rolepermissionrepository.GetAll().Where(x => x.RoleId == request.RoleId).ToList();
                    await rolepermissionrepository.DeleteRange(permissioninfo);


                    List<RolePermission> rolePermissions = new List<RolePermission>();
                    foreach (var item in request.PermissionId)
                    {
                        rolePermissions.Add(new RolePermission()
                        {
                            RoleId = request.RoleId,
                            PermissionId = item,
                            CreateTime = DateTime.Now,
                            Status = 1

                        });
                    }
                    await rolepermissionrepository.AddRange(rolePermissions);

                    tran.Commit();
                    return new ApiResult<int>()
                    {
                        Code = ApiResultEnum.修改成功
                        ,
                        Message = ApiResultEnum.修改成功.ToString(),
                        Data = res
                    };
                  

                }
                catch (Exception ex)
                {
                    logger.LogError("=>" + ex.Message);

                    tran.Rollback();
                }
                return new ApiResult<int>()
                {
                    Code = ApiResultEnum.修改失败
                   ,
                    Message = ApiResultEnum.修改失败.ToString(),
                    Data = -1
                };

            }
      

        }
    }
}
